package StatusOrder

import (
	LogUtils "IMServices/LogService"
	"IMServices/NetIO/Define"
	"IMServices/NetIO/Packet"
	ServiceNetIO2 "IMServices/NetIO/ServiceNetIO"
	"IMServices/StatusService/StatusCache"
	"encoding/json"
)

var OrderIdFunction map[int64]func(ServiceNetIO2.RequestBuffer)

func Init() {
	OrderIdFunction = make(map[int64]func(ServiceNetIO2.RequestBuffer))
	Register()
}

// Register 注册方法
func Register() {
	OrderIdFunction[Packet.MethodLogin] = CheckLogin
	OrderIdFunction[Packet.MethodDefault] = Test
}

// 对比用户信息缓存表，校验用户的用户名密码是否正确[目前只校验了密码]
func CheckLogin(data ServiceNetIO2.RequestBuffer) {
	LoginDefine := Define.LoginDefine{}
	json.Unmarshal(data.Request.Body, &LoginDefine)
	userInfoMap := StatusCache.LoginInfoMap
	CacheUser := userInfoMap[LoginDefine.Name]
	if LoginDefine.Password == CacheUser.Password {
		LogUtils.LogInfo(LoginDefine.Name + "登陆成功")
	} else {
		LogUtils.LogError(LoginDefine.Name + "密码错误，登录失败")
	}
}

// Test 处理函数
func Test(data ServiceNetIO2.RequestBuffer) {
	println(data.Request.OrderId)
	//处理结束返回数据包
	//var loginDefine Define.LoginDefine
	//err := json.Unmarshal(data.Request.Body, &loginDefine)
	//if err != nil {
	//	LogUtils.LogError("statusService 解析grpc包时出现异常")
	//}
	//fmt.Println(loginDefine)
	//测试发送 gRPC 请求
	//req := &ServiceNetIO2.NewMessageRequest{OrderId: 1}
	////res, err := client.NewNotify(data.Ctx, req)
	//if err != nil {
	//	log.Fatalf("failed to call YourMethod: %v", err)
	//}
	//log.Printf("response: %v", res)
}
